<template>
  <el-dialog :visible.sync="dialogTableVisible" :append-to-body="true" :lock-scroll="false" :title="isEdit?'Edit':'Create'" @close="handleCloseDetailDialog">
    <el-form ref="postForm" :model="postForm" :rules="rules" label-position="left" label-width="70px" style="margin-left:50px;">

      <el-form-item :label="$t('table.name')" prop="name" >
        <el-col :span="20">
          <el-input v-model="postForm.name" :label="$t('table.name')"/>
        </el-col>
      </el-form-item>

      <template v-if="isEdit">
        <el-form-item :label="$t('adminfiles.email')" prop="email" >
          <el-col :span="20">
            <el-input :disabled="true" v-model="postForm.email" :label="$t('adminfiles.email')"/>
          </el-col>
        </el-form-item>
      </template>
      <template v-else>
        <el-form-item :label="$t('adminfiles.email')" prop="email" >
          <el-col :span="20">
            <el-input v-model="postForm.email" :label="$t('adminfiles.email')"/>
          </el-col>
        </el-form-item>
      </template>

      <el-form-item :label="$t('table.password')" prop="password" >
        <el-col :span="20">
          <el-input v-model="postForm.password" :label="$t('table.password')"/>
        </el-col>
      </el-form-item>

    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCloseDetailDialog">{{ $t('table.cancel') }}</el-button>
      <el-button type="primary" @click="submitForm('postForm')">{{ $t('table.submit') }}</el-button>
    </div>

  </el-dialog>
</template>
<script>

import adminApi from '@/api/system/admin'
import ScrollPane from '../../../../../components/ScrollPane/index'

const defaultForm = {
  name: '',
  email: '',
  password: ''
}

export default {
  name: 'Detail',
  components: { ScrollPane },
  props: {
    isEdit: {
      type: Boolean,
      default: false,
      required: false
    },
    editId: {
      type: Number,
      default: 0,
      required: false
    }
  },
  data() {
    return {
      postForm: Object.assign({}, defaultForm),
      rules: {
        name: [
          { required: true, message: 'name is required', trigger: 'blur' }
        ],
        email: [
          { required: true, message: 'email is required', trigger: 'blur' }
        ],
        password: [
          { required: true, message: 'password is required', trigger: 'blur' }
        ]
      },
      areaList: [],
      imageUrl: '',
      recordid: 0,
      dialogTableVisible: false
    }
  },
  computed: {
    lang() {
      return this.$store.getters.language
    }
  },
  created() {
    if (this.isEdit) {
      const id = this.editId || (this.$route.params && this.$route.params.id)
      this.showRecord(id)
    } else {
      this.postForm = Object.assign({}, defaultForm)
    }
  },

  mounted() {

  },

  methods: {
    showRecord(id) {
      adminApi.showRecord(id).then(response => {
        const serviceResponse = response.data
        if (serviceResponse.code === 0) {
          this.postForm = serviceResponse.data
          this.imageUrl = this.postForm.imgpath + this.postForm.ad_image
        }
      }).catch(err => {
        console.log(err)
      })
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.editId > 0) {
            adminApi.updateRecord(this.editId, this.postForm).then(response => {
              const serviceResponse = response.data
              if (serviceResponse.code === 0) {
                this.$message({
                  type: 'success',
                  message: serviceResponse.msg
                })
                this.handleCloseDetailDialog()
              } else {
                this.$message({
                  type: 'error',
                  message: serviceResponse.msg
                })
              }
            })
          } else {
            adminApi.storeRecord(this.postForm).then(response => {
              const serviceResponse = response.data
              if (serviceResponse.code === 0) {
                this.$message({
                  type: 'success',
                  message: serviceResponse.msg
                })
                this.handleCloseDetailDialog()
              } else {
                this.$message({
                  type: 'error',
                  message: serviceResponse.msg
                })
              }
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(postForm) {
      this.$refs[postForm].resetFields()
    },

    handleImagesSuccess(res, file) {
      if (res.code === 0) {
        this.postForm.ad_image = res.data.paths[0].path
        const imgpath = res.data.imgpath
        this.imageUrl = imgpath + res.data.paths[0].path
      } else {
        alert(res.data.msg)
      }
    },
    handShowDetailDialog(id) {
      if (id > 0) {
        this.isEdit = true
        this.editId = id
        this.showRecord(id)
      } else {
        this.postForm = Object.assign({}, defaultForm)
      }

      this.dialogTableVisible = true
    },
    handleCloseDetailDialog() {
      this.resetForm('postForm')
      this.editId = 0
      this.$emit('indexRecord')
      this.dialogTableVisible = false
    }
  }

}
</script>
